<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="字段名称" prop="columnName">
                        <el-input v-model="form.columnName" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="字段注释" prop="columnComment">
                        <el-input v-model="form.columnComment" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="字段类型" prop="type">
                        <sc-select v-model="form.type" dict="DB_MYSQL_TYPE" clearable filterable style="width: 100%;"></sc-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="_length_show">
                <el-col :span="12">
                    <el-form-item label="字段长度" prop="length" :rules="_length_rules">
                        <sc-select v-model="form.length" dict="DB_MYSQL_TYPE_LEN" placeholder="长度" filterable style="width: 100%;"></sc-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6" v-if="_scale_show">
                    <el-form-item label="&nbsp;," prop="scale" :rules="_scale_rules" label-width="20px">
                        <el-input-number v-model="form.scale" placeholder="小数" :min="0" :max="20" step-strictly :step="1" controls-position="right"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="基础字段" prop="base">
                        <el-switch v-model="form.base"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="主键" prop="primaryKey">
                        <el-switch v-model="form.primaryKey"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="自增" prop="autoIncrement">
                        <el-switch v-model="form.autoIncrement"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="不是null" prop="nullable">
                        <el-switch v-model="form.nullable"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="无符号" prop="unsigned">
                        <el-switch v-model="form.unsigned"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="默认值" prop="defaultValue">
                        <el-input v-model="form.defaultValue" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" type="textarea" maxlength="255" show-word-limit/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!=='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
    emits: ['success', 'closed'],
    data() {
        const checkExist = (rule, value, callback) => {
            if (!value || value === '') {
                callback() //条件不满足 也需要回调、返回，此次回调成功，因为外面有其他规则校验
                return;
            }
            this.exists(rule.field, value).then(res => {
                if (res.code !== 200) {
                    callback(new Error(res.msg)) // 返回校验失败
                } else if (res.data) {
                    callback(new Error('字段已存在')) // 返回校验失败
                } else {
                    callback()
                }
            })
        }
        return {
            mode: "add",
            titleMap: {
                add: '新增字典项',
                edit: '编辑字典项',
                show: '查看字典项'
            },
            visible: false,
            isSaveing: false,
            //表单数据
            form: {
                id: "",
                columnName: "",
                columnComment: "",
                type: "",
                length: "",
                scale: "",
                base: false,
                primaryKey: false,
                autoIncrement: false,
                nullable: false,
                unsigned: false,
                defaultValue: "",
                remark: ""
            },
            //验证规则
            rules: {
                columnName: [
                    {required: true, message: '请输入字段名称'}, {validator: checkExist, trigger: 'blur'}
                ],
                columnComment: [
                    {required: true, message: '请输入字段注释'}
                ],
                type: [
                    {required: true, message: '请选择字段类型'}
                ]
            }
        }
    },
    mounted() {

    },
    computed: {
        _length_rules() {
            return {required: this._length_show, message: '请输入字段长度'}
        },
        _scale_rules() {
            return {required: this._scale_show, message: '请输入小数长度'}
        },
        _length_show() {
            return this._scale_show || 'STRING' === this.form.type
        },
        _scale_show() {
            return 'DECIMAL' === this.form.type
        }
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    const res = this.mode === 'add' ? await this.$API.design.projectDict.post(this.form) : await this.$API.design.projectDict.put(this.form);
                    this.isSaveing = false;
                    if (res.code === 200) {
                        this.$emit('success', this.form, this.mode)
                        this.visible = false;
                        this.$message.success("操作成功")
                    } else {
                        await this.$alert(res.msg, "提示", {type: 'error'})
                    }
                }
            })
        },
        // 验证字段是否存在
        async exists(name, value) {
            const fieldNames = ['projectId', name]
            const fieldValues = [this.form.projectId, value]
            return await this.$API.design.projectDict.exists(fieldNames.join(), fieldValues.join(), this.form.id);
        },
        //表单注入数据
        setData(data) {
            this.form.id = data.id ? data.id : ''
            this.form.projectId = data.projectId
            this.form.columnName = data.columnName
            this.form.columnComment = data.columnComment
            this.form.type = data.type
            this.form.length = data.length
            this.form.scale = data.scale
            this.form.base = data.base
            this.form.primaryKey = data.primaryKey
            this.form.autoIncrement = data.autoIncrement
            this.form.nullable = data.nullable
            this.form.unsigned = data.unsigned
            this.form.defaultValue = data.defaultValue
            this.form.remark = data.remark
            //可以和上面一样单个注入，也可以像下面一样直接合并进去
            //Object.assign(this.form, data)
        }
    }
}
</script>

<style>
</style>
